package cn.iocoder.yudao.module.mini.controller.admin.opinion;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mini.controller.admin.opinion.vo.*;
import cn.iocoder.yudao.module.mini.dal.dataobject.opinion.OpinionDO;
import cn.iocoder.yudao.module.mini.convert.opinion.OpinionConvert;
import cn.iocoder.yudao.module.mini.service.opinion.OpinionService;

@Tag(name = "管理后台 - 意见反馈")
@RestController
@RequestMapping("/mini/opinion")
@Validated
public class OpinionController {

    @Resource
    private OpinionService opinionService;

    @PostMapping("/create")
    @Operation(summary = "创建意见反馈")
    @PreAuthorize("@ss.hasPermission('mini:opinion:create')")
    public CommonResult<Long> createOpinion(@Valid @RequestBody OpinionCreateReqVO createReqVO) {
        return success(opinionService.createOpinion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新意见反馈")
    @PreAuthorize("@ss.hasPermission('mini:opinion:update')")
    public CommonResult<Boolean> updateOpinion(@Valid @RequestBody OpinionUpdateReqVO updateReqVO) {
        opinionService.updateOpinion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除意见反馈")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mini:opinion:delete')")
    public CommonResult<Boolean> deleteOpinion(@RequestParam("id") Long id) {
        opinionService.deleteOpinion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得意见反馈")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mini:opinion:query')")
    public CommonResult<OpinionRespVO> getOpinion(@RequestParam("id") Long id) {
        OpinionDO opinion = opinionService.getOpinion(id);
        return success(OpinionConvert.INSTANCE.convert(opinion));
    }

    @GetMapping("/list")
    @Operation(summary = "获得意见反馈列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mini:opinion:query')")
    public CommonResult<List<OpinionRespVO>> getOpinionList(@RequestParam("ids") Collection<Long> ids) {
        List<OpinionDO> list = opinionService.getOpinionList(ids);
        return success(OpinionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得意见反馈分页")
    @PreAuthorize("@ss.hasPermission('mini:opinion:query')")
    public CommonResult<PageResult<OpinionRespVO>> getOpinionPage(@Valid OpinionPageReqVO pageVO) {
        PageResult<OpinionDO> pageResult = opinionService.getOpinionPage(pageVO);
        return success(OpinionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出意见反馈 Excel")
    @PreAuthorize("@ss.hasPermission('mini:opinion:export')")
    @OperateLog(type = EXPORT)
    public void exportOpinionExcel(@Valid OpinionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OpinionDO> list = opinionService.getOpinionList(exportReqVO);
        // 导出 Excel
        List<OpinionExcelVO> datas = OpinionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "意见反馈.xls", "数据", OpinionExcelVO.class, datas);
    }

}
